package com.ruoyi.system.service.impl;

import com.ruoyi.system.mapper.GoodsCategoryMapper;
import com.ruoyi.system.domain.GoodsCategory;
import com.ruoyi.system.service.GoodsCategoryService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * (GoodsCategory)表服务实现类
 *
 * @author makejava
 * @since 2020-10-27 10:57:19
 */
@Service("goodsCategoryService")
public class GoodsCategoryServiceImpl implements GoodsCategoryService {
    @Resource
    private GoodsCategoryMapper goodsCategoryDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public GoodsCategory queryById(Integer id) {
        return this.goodsCategoryDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @return 对象列表
     */
    @Override
    public List<GoodsCategory> queryAllByLimit() {
        List<GoodsCategory> goodsCategories = this.goodsCategoryDao.queryAllByLimit();
        return goodsCategories.stream()
                .sorted(Comparator.comparing(GoodsCategory::getSort))
                .collect(Collectors.toList());
    }

    /**
     * 新增数据
     *
     * @param goodsCategory 实例对象
     * @return 实例对象
     */
    @Override
    public GoodsCategory insert(GoodsCategory goodsCategory) {
        this.goodsCategoryDao.insert(goodsCategory);
        return goodsCategory;
    }

    /**
     * 修改数据
     *
     * @param goodsCategory 实例对象
     * @return 实例对象
     */
    @Override
    public GoodsCategory update(GoodsCategory goodsCategory) {
        this.goodsCategoryDao.update(goodsCategory);
        return this.queryById(goodsCategory.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.goodsCategoryDao.deleteById(id) > 0;
    }
}
